<script type="text/html" template lay-done="layui.data.done(d);">
    <div class="table-body">
        <table id="LAY-app-CoreCmsUserServicesTicket-tableBox" lay-filter="LAY-app-CoreCmsUserServicesTicket-tableBox"></table>
    </div>
    <script type="text/html" id="LAY-app-CoreCmsUserServicesTicket-toolbar">
        <div class="layui-form coreshop-toolbar-search-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="redeemCode" placeholder="请输入兑换码" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="">请选择类型</option>
                            {{# layui.each(d.params.data.servicesTicketStatus, function(index, item){ }}
                            <option value="{{ item.value }}">{{ item.description }}</option>
                            {{# }); }}
                        </select>
                    </div>
                </div>
                <!--<div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="validityType" placeholder="请输入核销有效期类型" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 260px;">
                        <input type="text" name="validityStartTime" id="searchTime-CoreCmsUserServicesTicket-validityStartTime" placeholder="请输入核销开始时间" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 260px;">
                        <input type="text" name="validityEndTime" id="searchTime-CoreCmsUserServicesTicket-validityEndTime" placeholder="请输入核销结束时间" class="layui-input">
                    </div>
                </div>
               <div class="layui-inline">
                    <label class="layui-form-label" for="createTime">创建时间</label>
                    <div class="layui-input-inline" style="width: 260px;">
                        <input type="text" name="createTime" id="searchTime-CoreCmsUserServicesTicket-createTime" placeholder="请输入创建时间" class="layui-input">
                    </div>
                </div>-->
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <select name="isVerification">
                            <option value="">请选择是否核销</option>
                            <option value="True">是</option>
                            <option value="False">否</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 260px;">
                        <input type="text" name="verificationTime" id="searchTime-CoreCmsUserServicesTicket-verificationTime" placeholder="请输入核销时间" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsUserServicesTicket-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
                </div>
            </div>
        </div>
    </script>


</script>


<script type="text/html" id="LAY-app-CoreCmsUserServicesTicket-pagebar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="selectExportExcel"><i class="layui-icon layui-icon-add-circle"></i>选择导出</button>
        <button class="layui-btn layui-btn-sm" lay-event="queryExportExcel"><i class="layui-icon layui-icon-download-circle"></i>查询导出</button>
    </div>
</script>

<script type="text/html" id="LAY-app-CoreCmsUserServicesTicket-tableBox-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs"  lay-event="cancellation">作废</a>
</script>

<script>
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper'], function () {
            var $ = layui.$
                , admin = layui.admin
                , table = layui.table
                , form = layui.form
                , laydate = layui.laydate
                , setter = layui.setter
                , coreHelper = layui.coreHelper
                , util = layui.util
                , view = layui.view;

            var searchwhere;
            //监听搜索
            form.on('submit(LAY-app-CoreCmsUserServicesTicket-search)',
                function (data) {
                    var field = data.field;
                    searchwhere = field;
                    //执行重载
                    table.reloadData('LAY-app-CoreCmsUserServicesTicket-tableBox', { where: field });
                });
            //数据绑定
            table.render({
                elem: '#LAY-app-CoreCmsUserServicesTicket-tableBox',
                url: layui.setter.apiUrl + 'Api/CoreCmsUserServicesOrder/GetTicketPageList?serviceOrderId=' + d.params.data.model.serviceOrderId,
                method: 'POST',
                toolbar: '#LAY-app-CoreCmsUserServicesTicket-toolbar',
                pagebar: '#LAY-app-CoreCmsUserServicesTicket-pagebar',
                className: 'pagebarbox',
                defaultToolbar: ['filter', 'print', 'exports'],
                height: 'full-227',//面包屑142px,搜索框4行172,3行137,2行102,1行67
                page: true,
                limit: 30,
                limits: [10, 15, 20, 25, 30, 50, 100, 200],
                text: { none: '暂无相关数据' },
                cols: [
                    [
                        { type: "checkbox", fixed: "left" },
                        { field: 'id', title: '序列', width: 50, sort: false },
                        //{ field: 'serviceOrderId', title: '关联购买订单', sort: false, width: 105 },
                        { field: 'securityCode', title: '安全码', sort: false, width: 155 },
                        { field: 'redeemCode', title: '兑换码', sort: false, width: 70 },
                        //{ field: 'serviceId', title: '关联服务项目id', sort: false, width: 105 },
                        //{ field: 'userId', title: '关联用户id', sort: false, width: 105 },
                        {
                            field: 'status', title: '状态', sort: false, width: 70, templet: function (data) {
                                for (var i = 0; i < d.params.data.servicesTicketStatus.length; i++) {
                                    if (data.status == d.params.data.servicesTicketStatus[i].value) {
                                        return d.params.data.servicesTicketStatus[i].description;
                                    }
                                }
                                return "";
                            }
                        },
                        //{ field: 'validityType', title: '核销有效期类型', sort: false, width: 105 },
                        {
                            field: 'validityType', title: '有效期类型', sort: false, width: 80,
                            templet: function (data) {
                                for (var j = 0; j < d.params.data.types.length; j++) {
                                    if (d.params.data.types[j].value == data.validityType) {
                                        return d.params.data.types[j].description;
                                    }
                                }
                                return "";
                            }
                        },
                        { field: 'validityStartTime', title: '核销开始时间', width: 130, sort: false },
                        { field: 'validityEndTime', title: '核销结束时间', width: 130, sort: false },
                        { field: 'createTime', title: '创建时间', width: 130, sort: false },
                        { field: 'isVerification', title: '是否核销', width: 65, templet: '#switch_isVerification', sort: false, unresize: true },
                        { field: 'verificationTime', title: '核销时间', width: 130, sort: false },
                        { width: 60, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsUserServicesTicket-tableBox-bar' }
                    ]
                ]
            });
            //监听工具条
            table.on('tool(LAY-app-CoreCmsUserServicesTicket-tableBox)',
                function(obj) {
                    if (obj.event === 'cancellation') {
                        doCancellation(obj);
                    } 
                });

            //头工具栏事件
            table.on('pagebar(LAY-app-CoreCmsUserServicesTicket-tableBox)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                case 'selectExportExcel':
                        doSelectExportExcel(checkStatus);
                        break;
                    case 'queryExportExcel':
                        doQueryExportexcel();
                        break;
                };
            });

            //作废核销码记录
            function doCancellation(obj) {
                layer.confirm('确定要作废当前核销码吗？',
                    function (index) {
                        coreHelper.Post("Api/CoreCmsUserServicesOrder/DoCancellationTicket", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            table.reloadData('LAY-app-CoreCmsUserServicesTicket-tableBox');
                            layer.msg(e.msg);
                        });
                    });
            }
            //执行查询条件导出excel
            function doQueryExportexcel() {
                layer.confirm('确定根据当前的查询条件导出数据吗？',
                    function (index) {
                        var field = searchwhere;
                        coreHelper.PostForm("Api/CoreCmsUserServicesOrder/QueryTicketExportExcel", field, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            if (e.code === 0) {
                                window.open(e.data);
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    });
            }
            //执行选择目录导出数据
            function doSelectExportExcel(checkStatus) {
                var checkData = checkStatus.data;
                if (checkData.length === 0) {
                    return layer.msg('请选择您要导出的数据');
                }
                layer.confirm('确定导出选择的内容吗？',
                    function (index) {
                        var delidsStr = [];
                        layui.each(checkData,
                            function (index, item) {
                                delidsStr.push(item.id);
                            });
                        layer.close(index);
                        coreHelper.Post("Api/CoreCmsUserServicesOrder/SelectTicketExportExcel", { id: delidsStr }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            if (e.code === 0) {
                                window.open(e.data);
                            } else {
                                layer.msg(e.msg);
                            }
                        });
                    });
            }

            laydate.render({
                elem: '#searchTime-CoreCmsUserServicesTicket-validityStartTime',
                type: 'datetime',
                range: '到',
            });
            laydate.render({
                elem: '#searchTime-CoreCmsUserServicesTicket-validityEndTime',
                type: 'datetime',
                range: '到',
            });
            laydate.render({
                elem: '#searchTime-CoreCmsUserServicesTicket-createTime',
                type: 'datetime',
                range: '到',
            });
            laydate.render({
                elem: '#searchTime-CoreCmsUserServicesTicket-verificationTime',
                type: 'datetime',
                range: '到',
            });

            //监听 表格复选框操作

            layui.form.on('switch(switch_isVerification)', function (obj) {
                coreHelper.Post("Api/CoreCmsUserServicesOrder/DoSetisVerification", { id: this.value, data: obj.elem.checked }, function (e) {
                    if (debug) { console.log(e); } //开启调试返回数据
                    //table.reloadData('LAY-app-CoreCmsUserServicesTicket-tableBox');
                    layer.msg(e.msg);
                });
            });


            form.render();
        });
    };
</script>



<!--设置是否核销-->
<script type="text/html" id="switch_isVerification">
    <input type="checkbox" name="switch_isVerification" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="switch_isVerification" {{ d.isVerification ? 'checked' : '' }}>
</script>
